<?php
namespace Home\Controller;
use Think\Controller;
class ApiController extends Controller {
	
    public function index(){
	
	
    }
		
//===================================================================
	//老系统对接
	public function pd_addless(){
		$this->display();
	}
	
	public function get_addless_lxt(){
		if($_POST){
			$p=I('post.');
			$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$p['longitude'].",".$p['latitude']."&from=1&to=5&ak=".C('BB_AK');
	 		$re = get_curl($url);
			$zb = json_decode($re,true);
			if($zb['status']==0){
				$longitude=$zb['result']['0']['x'];
				$latitude=$zb['result']['0']['y'];
				$url = 'http://api.map.baidu.com/geocoder?location='.$latitude.','.$longitude.'&coord_type=bd09ll&output=json&src=jt';
				$addressData = get_curl($url);
				$address = json_decode($addressData,true);
				echo $address['result']['addressComponent']['city'];
			}
		}
	}
//===================================================================	
	//百度坐标转换，位置名称
	public function get_position(){
		if($_POST){
			$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$_POST['longitude'].",".$_POST['latitude']."&from=1&to=5&ak=".C('BB_AK');
 			$re = get_curl($url);
			$zb = json_decode($re,true);
			$url = 'http://api.map.baidu.com/geocoder?location='.$zb['result'][0]['y'].','.$zb['result'][0]['x'].'&coord_type=gcj02&output=json&src=jt';
			$addressData = get_curl($url);
			$address = json_decode($addressData,true);
			$data['z']=$zb;
			$data['d']=$address;
			echo json_encode($data);
		}
	}
	
	//百度坐标转换工具，gps位置转换百度坐标
	public function bd_geoconv(){
		if($_POST){
			$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$_POST['longitude'].",".$_POST['latitude']."&from=1&to=5&ak=".C('BB_AK');
 			$re = get_curl($url);
			$zb = json_decode($re,true);
			$this->ajaxReturn($zb,'ok',1);
		}else{
			$this->ajaxReturn(0,'失败！',1);
		}
	}
	
	public function bd_geocoder(){
		if($_POST){
			$url = 'http://api.map.baidu.com/geocoder?location='.$_POST['longitude'].','.$_POST['latitude'].'&coord_type=gcj02&output=json&src=jt';
			$addressData = get_curl($url);
			$address = json_decode($addressData,true);
			$this->ajaxReturn($address,'ok',1);
		}else{
			$this->ajaxReturn(0,'失败！',1);
		}
	}
	
	

//百度api接口===========================================================================================
	public function get_coordinate_position_wap(){
		if($_POST){
			$p=I('post.');
			$zb=$this->get_coordinate_pc($p);
			$wz=$this->get_position_pc($zb);
			$data['status'] = 1;
			$data['msg'] = 'ok';
			$data['data'] = $wz;
		}else{
			$data['status']  = 0;
			$data['msg'] = '没有找到你的坐标';
		}
		$this->ajaxReturn($data);
	}
	//百度根据坐标找位置
	private function get_position_pc($zb=''){
		if($zb){
			$url = 'http://api.map.baidu.com/geocoder?location='.$zb['latitude'].','.$zb['longitude'].'&coord_type=bd09ll&output=json&src=jt';
			$addressData = get_curl($url);
			$address = json_decode($addressData,true);
			if($address['status']=='OK'){
				$addres['address']=$address['result']['formatted_address'];
				$addres['longitude']=$address['result']['location']['lng'];
				$addres['latitude']=$address['result']['location']['lat'];
				return $addres;
			}else{
				return FALSE;
			}
		}
	}
	//百度坐标转换,$zb=原坐标
	private function get_coordinate_pc($zb='',$from=1){
		if($zb){
			$url = "http://api.map.baidu.com/geoconv/v1/?coords=".$zb['longitude'].",".$zb['latitude']."&from=".$from."&to=5&ak=".C('BB_AK');
	 		$re = get_curl($url);
			$zb = json_decode($re,true);
			if($zb['status']==0){
				$lszb['longitude']=$zb['result']['0']['x'];
				$lszb['latitude']=$zb['result']['0']['y'];
				return $lszb;
			}else{
				return FALSE;
			}
		}
	}
}